package com.yourname.xhs.dao;

import com.yourname.xhs.bean.Announce;
import com.yourname.xhs.bean.Note;
import com.yourname.xhs.entity.User;
import com.yourname.xhs.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AnnounceDaoImpl implements AnnounceDao{
      Connection conn=null;
      PreparedStatement ps= null;
      ResultSet rs=null;


      //发布通告
    public boolean createAnnounce(Announce announce) throws SQLException {
        boolean i;
        try {
            conn=DbUtil.getConnection();

            String sql="insert into announce (content) values (?)";
            ps=conn.prepareStatement(sql);
            ps.setString(1,announce.getContent());
            int line=ps.executeUpdate();
            if(line>0){
                i=true;
            }
            else {
                i=false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(rs,ps,conn);
        }

        return i;
    }



    //删除通告
    public int deleteAnnounce(Long id){
        int line;
        try {
            conn = DbUtil.getConnection();
            String sql = "delete from announce where id=?";
            ps=conn.prepareStatement(sql);
            ps.setLong(1,id);
            line=ps.executeUpdate();


        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
      return line;
    }


    //查看通告
    public List<Announce> selectAnnounce() throws Exception {
        List<Announce> announceList = new ArrayList<>();
        try{
            conn= DbUtil.getConnection();
            String sql="SELECT id,content from announce";
            ps = conn.prepareStatement(sql);
            rs=  ps.executeQuery();
            while (rs.next()) {
                Long id = rs.getLong("id");
                String content = rs.getString("content");
                Announce announce=new Announce();
                announce.setId(id);
                announce.setContent(content);
                announceList.add(announce);
            }

        }
        finally {

            DbUtil.close(rs,ps,conn);
        }
        return announceList;
    }









}
